#region using

using System.IO;
using MvcMall.Data;
using MvcMall.Data.NHibernateMaps;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;
using SharpArch.Data.NHibernate;
using SharpArch.Testing.NHibernate;

#endregion

namespace MvcMall.Tests.MvcMallData.NHibernateMaps
{
    /// <summary>
    ///     Provides a means to verify that the target database is in compliance with all mappings.
    ///     Taken from http://ayende.com/Blog/archive/2006/08/09/NHibernateMappingCreatingSanityChecks.aspx.
    /// 
    ///     If this is failing, the error will likely inform you that there is a missing table or column
    ///     which needs to be added to your database.
    /// </summary>
    [TestFixture]
    [Category("DB Schema Tests")]
    public class FluentMappingIntegrationTests
    {
         #region Setup/Teardown

        [SetUp]
        public virtual void SetUp()
        {
            var mappingAssemblies = RepositoryTestsHelper.GetMappingAssemblies();
            _nhConfig = NHibernateSession.Init(new SimpleSessionStorage(), mappingAssemblies,
                                               new AutoPersistenceModelGenerator().Generate(),
                                               TEST_DB_CONFIG_PATH);
        }

        [TearDown]
        public virtual void TearDown()
        {
            NHibernateSession.CloseAllSessions();
            NHibernateSession.Reset();
        }

        #endregion

/*
        private static SchemaUpdate PrepareSchemaUpdate()
        {
            return new SchemaUpdate(_nhConfig);
        }
*/

        private static SchemaExport PrepareSchemaExport()
        {
            return new SchemaExport(_nhConfig);
        }

        private const string TEST_DB_CONFIG_PATH = "../../../../tests/ENS.Tests/HibernateSQL8.cfg.xml";
        private const string DB_FOLDER_PATH = "../../../../db/";

        private static Configuration _nhConfig;

        [Test]
        public void ExportSchemaToFile()
        {
            PrepareSchemaExport().Execute(true, false, false, null, new StreamWriter(DB_FOLDER_PATH + "schema/DbSchema.sql"));
        }

/*
        [Test]
        public void UpdateLiveDbSchema()
        {
            PrepareSchemaUpdate().Execute(true, false);
        }
*/

        [Test]
        public void WriteMappingsToFile()
        {
            var mappings = new AutoPersistenceModelGenerator().Generate();
            mappings.BuildMappings();
            mappings.WriteMappingsTo(DB_FOLDER_PATH + "maps/");
        }
    }
}